﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class SpecialPlan_AddSpecialPlan : System.Web.UI.Page
{
    Model.UserInfo objUserInfo = new Model.UserInfo();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDrp(this.drpDepartment);
            BindDrp(this.drpMasterPeople);
            BindGdv();
            if (objUserInfo.UserName == "admin")
            {
                this.btnImport.Visible = true;
            }
            else
            {
                this.btnImport.Visible = false;
            }
        }
    }

    /// <summary>
    /// 绑定下拉部门
    /// </summary>
    void BindDrp(DropDownList _drp)
    {
        //部门绑定
        if (_drp.Equals(this.drpDepartment))
        {
            if (objUserInfo.UserName != "admin")
            {
                object[] _value = new object[1];

                _value[0] = objUserInfo.UserName;
                DataTable _userTable = new BLL.UserInfoManager().get(BLL.UserInfoManager.field.UserName.ToString(), _value);
                if (_userTable.Rows.Count > 0)
                {
                    if (_userTable.Rows[0]["DepartmentId"].ToString().Length > 0)
                    {
                        ListItem _li = new ListItem(_userTable.Rows[0]["DepartmentId"].ToString(), _userTable.Rows[0]["DId"].ToString());
                        this.drpDepartment.Items.Add(_li);
                    }
                    if (_userTable.Rows[0]["PartTimeDepartmentId"].ToString().Length > 0)
                    {
                        ListItem _li = new ListItem(_userTable.Rows[0]["PartTimeDepartmentId"].ToString(), _userTable.Rows[0]["PDid"].ToString());
                        this.drpDepartment.Items.Add(_li);
                    }
                }
            }
            else
            {
                this.drpDepartment.DataSource = new BLL.DepartmentInfoManager().getNoTree();
                this.drpDepartment.DataTextField = "DepartmentName";
                this.drpDepartment.DataValueField = "DepartmentId";
                this.drpDepartment.DataBind();
                ListItem _li = new ListItem("请选择部门");
            }
        }
        //绑定责任人
        if (_drp.Equals(this.drpMasterPeople))
        {
            object[] _value = new object[1];
            _value[0] = Convert.ToInt32(this.drpDepartment.SelectedItem.Value);
            BLL.DepartmentInfoManager dllDepartmentInfoManager = new BLL.DepartmentInfoManager();
            DataTable dtdepart = new BLL.DepartmentInfoManager().get(BLL.DepartmentInfoManager.field.DepartmentId.ToString(), _value);
            DataTable dt = new BLL.UserInfoManager().get(BLL.UserInfoManager.field.DepartmentId.ToString(), _value);
            this.drpMasterPeople.DataSource = dt;
            this.drpMasterPeople.DataTextField = "RealName";
            this.drpMasterPeople.DataValueField = "UserName";
            this.drpMasterPeople.DataBind();
            if (dtdepart.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    this.drpMasterPeople.SelectedIndex = this.drpMasterPeople.Items.IndexOf(this.drpMasterPeople.Items.FindByValue(dtdepart.Rows[0]["DepartmentMaster"].ToString()));
                    this.txtMasterPeople.Value = this.drpMasterPeople.SelectedItem.Text;
                }
            }
        }

    }

    /// <summary>
    /// 绑定专项计划列表
    /// </summary>
    void BindGdv()
    {
        object[] _value = new object[1];
        _value[0] = this.drpDepartment.SelectedItem.Text;
        DataTable dt = new BLL.SpecialPlan().GetSpecialPlanByDepartment(_value);
        this.gdvSpecialPlan.DataSource = dt;
        this.gdvSpecialPlan.DataBind();
        for (int i = 0; i < this.gdvSpecialPlan.Rows.Count; i++)
        {
            wrapByComma(this.gdvSpecialPlan, i, 6);
            if (Convert.ToInt32(gdvSpecialPlan.DataKeys[i]["State"].ToString()) == 1
                    || Convert.ToInt32(gdvSpecialPlan.DataKeys[i]["State"].ToString()) == 2
                    || Convert.ToInt32(gdvSpecialPlan.DataKeys[i]["State"].ToString()) == 9)
            {
                gdvSpecialPlan.Rows[i].BackColor = System.Drawing.Color.Pink;
            }
        }
        this.txtSpecialPlanSortId.Value = (this.gdvSpecialPlan.Rows.Count + 1).ToString();
    }

    /// <summary>
    /// 创建空白Table
    /// </summary>
    /// <returns></returns>
    DataTable CreateDataTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Id");
        dt.Columns.Add("SpecialPlanSortId");
        dt.Columns.Add("WorkItem");
        dt.Columns.Add("WorkItemDesc");
        dt.Columns.Add("StartTime");
        dt.Columns.Add("CompleteTime");
        dt.Columns.Add("MasterDepartment");
        dt.Columns.Add("MasterPeople");
        dt.Columns.Add("SlaveDepartment");
        dt.Columns.Add("SlaveDepartmentDuty");
        dt.Columns.Add("EvaluatePeople");
        dt.Columns.Add("CheckNote");
        dt.Columns.Add("EvaluatePeopleVerifyOpinion");
        dt.Columns.Add("State");
        dt.Columns.Add("PriorityLevel");
        dt.Columns.Add("CompleteState");
        dt.Columns.Add("CompleteDesc");
        dt.Columns.Add("VerifyPeople");
        dt.Columns.Add("VerifyCircuitId");
        dt.Columns.Add("Sponsor");
        dt.Columns.Add("AttachmengPath");
        dt.Columns.Add("Remart");
        return dt;
    }

    int isNull()
    {
        #region 验证是否为空
        if (this.txtSpecialPlanSortId.Value.Length == 0)
        {
            return 0;
        }
        if (this.txtCompleteTime.Text.Length == 0)
        {
            return 0;
        }
        if (this.txtMasterPeople.Value.Length == 0)
        {
            return 0;
        }
        if (this.txtWorkItem.Text.Length == 0)
        {
            return 0;
        }
        if (this.txtSlaveDepartment.Value.Length == 0)
        {
            return 0;
        }
        if (this.txtSlaveDepartmentDuty.Text.Length < 18 && this.txtSlaveDepartment.Value != "独立完成")
        {
            return 0;
        }
        if (this.txtEvaluatePeople.Value.Length == 0)
        {
            return 0;
        }


        return 1;
        #endregion
    }



    /// <summary>
    /// 添加事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        int _isnull = isNull();
        if (_isnull == 0)
        {
            Utility.HttpHelp.alert("请输入完整", Page);
        }
        else
        {
            if (Convert.ToDateTime(this.txtStartTime.Text) > Convert.ToDateTime(this.txtCompleteTime.Text))
            {
                Utility.HttpHelp.alert("开始时间不能大于结束时间", Page);
                return;
            }
            if (this.txtSlaveDepartment.Value != "独立完成")
            {
                if (this.txtSlaveDepartmentDuty.Text.Length < 18)
                {
                    Utility.HttpHelp.alert("配合部门责任字数不得低于32个字符", Page);
                    return;
                }
            }
            DataTable specialPlan = CreateDataTable();
            //获取当前gridview的内容导入到datatable
            for (int i = 0; i < this.gdvSpecialPlan.Rows.Count; i++)
            {
                DataRow dr = specialPlan.NewRow();
                dr["Id"] = this.gdvSpecialPlan.DataKeys[i]["Id"].ToString();
                dr["SpecialPlanSortId"] = this.gdvSpecialPlan.Rows[i].Cells[1].Text;
                dr["WorkItem"] = this.gdvSpecialPlan.Rows[i].Cells[2].Text;
                dr["WorkItemDesc"] = string.Empty;
                dr["StartTime"] = this.gdvSpecialPlan.Rows[i].Cells[3].Text;
                dr["CompleteTime"] = this.gdvSpecialPlan.Rows[i].Cells[4].Text;
                dr["MasterDepartment"] = this.drpDepartment.SelectedItem.Text;
                dr["MasterPeople"] = this.gdvSpecialPlan.Rows[i].Cells[5].Text;
                dr["SlaveDepartment"] = this.gdvSpecialPlan.Rows[i].Cells[6].Text;
                dr["SlaveDepartmentDuty"] = this.gdvSpecialPlan.Rows[i].Cells[7].Text;
                dr["EvaluatePeople"] = this.gdvSpecialPlan.Rows[i].Cells[8].Text;
                dr["CheckNote"] = string.Empty;
                dr["EvaluatePeopleVerifyOpinion"] = string.Empty;
                dr["State"] = Convert.ToInt32(this.gdvSpecialPlan.DataKeys[i]["State"]);
                dr["CompleteState"] = 0;
                dr["CompleteDesc"] = string.Empty;
                dr["VerifyPeople"] = string.Empty;
                dr["VerifyCircuitId"] = 0;
                dr["Sponsor"] = objUserInfo.UserName;
                dr["AttachmengPath"] = string.Empty;
                dr["PriorityLevel"] = this.gdvSpecialPlan.Rows[i].Cells[9].Text;
                dr["Remart"] = this.gdvSpecialPlan.Rows[i].Cells[10].Text;
                specialPlan.Rows.Add(dr);
            }
            //获取当前添加的信息内容
            DataRow newdr = specialPlan.NewRow();
            newdr["Id"] = string.Empty;
            newdr["SpecialPlanSortId"] = this.txtSpecialPlanSortId.Value;
            newdr["WorkItem"] = this.txtWorkItem.Text;
            newdr["WorkItemDesc"] = string.Empty;
            newdr["StartTime"] = Convert.ToDateTime(this.txtStartTime.Text).ToShortDateString();
            newdr["CompleteTime"] = Convert.ToDateTime(this.txtCompleteTime.Text).ToShortDateString();
            newdr["MasterDepartment"] = this.drpDepartment.SelectedItem.Text;
            newdr["MasterPeople"] = this.txtMasterPeople.Value;
            newdr["SlaveDepartment"] = this.txtSlaveDepartment.Value;
            newdr["SlaveDepartmentDuty"] = this.txtSlaveDepartmentDuty.Text;
            newdr["EvaluatePeople"] = this.txtEvaluatePeople.Value;
            newdr["CheckNote"] = string.Empty;
            newdr["EvaluatePeopleVerifyOpinion"] = string.Empty;
            newdr["State"] = 0;
            newdr["PriorityLevel"] = this.drpPriorityLevel.SelectedItem.Text;
            newdr["CompleteState"] = 0;
            newdr["CompleteDesc"] = string.Empty;
            newdr["VerifyPeople"] = string.Empty;
            newdr["VerifyCircuitId"] = 0;
            newdr["Sponsor"] = objUserInfo.UserName;
            newdr["AttachmengPath"] = string.Empty;
            newdr["Remart"] = this.txtRemark.Text;
            specialPlan.Rows.Add(newdr);
            DataView dv = new DataView(specialPlan);
            dv.Sort = "SpecialPlanSortId";
            this.gdvSpecialPlan.DataSource = dv;
            this.gdvSpecialPlan.DataBind();

            for (int i = 0; i < this.gdvSpecialPlan.Rows.Count; i++)
            {
                wrapByComma(this.gdvSpecialPlan, i, 6);
                if (Convert.ToInt32(gdvSpecialPlan.DataKeys[i]["State"].ToString()) == 1
                    || Convert.ToInt32(gdvSpecialPlan.DataKeys[i]["State"].ToString()) == 2
                    || Convert.ToInt32(gdvSpecialPlan.DataKeys[i]["State"].ToString()) == 9)
                {
                    gdvSpecialPlan.Rows[i].BackColor = System.Drawing.Color.Pink;
                }
            }
            #region 善后操作
            //将文本框清空
            this.txtSpecialPlanSortId.Value = Convert.ToString(this.gdvSpecialPlan.Rows.Count + 1);
            this.txtCompleteTime.Text = "";
            this.txtRemark.Text = "";
            this.txtSlaveDepartment.Value = "";
            this.txtSlaveDepartmentDuty.Text = "";
            this.txtEvaluatePeople.Value = "";
            this.txtWorkItem.Text = "";

            #endregion
        }


    }



    /// <summary>
    /// 根据逗号换行事件
    /// </summary>
    void wrapByComma(GridView _gridView, int _rowIndex, int _cells)
    {
        string _content = _gridView.Rows[_rowIndex].Cells[_cells].Text;
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&lt;br&gt;", "<br>");
        //_gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&gt;", ">");
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _gridView.Rows[_rowIndex].Cells[_cells].Text.Replace(",", "<br>");


    }

    /// <summary>
    /// 添加专项计划方法
    /// </summary>
    /// <param name="state">0草稿 1 提交</param>    
    void addSpecialPlan(int state, bool IsSubmit)
    {
        #region 参数
        int _count = 0;
        List<object[]> _valueList = new List<object[]>();
        //得到参数 部门名称的DataTable
        object[] _valueDepartmentName = new object[1];
        _valueDepartmentName[0] = this.drpDepartment.SelectedItem.Text;
        DataTable _VerifyCircuitId = new BLL.VerifyCircuitManager().getVerifyCircuitIdByDepartment(_valueDepartmentName);
        if (_VerifyCircuitId.Rows.Count == 0)
        {
            Utility.Error.RedirectErrorPage("所在部门无计划流程，请重试");
        }

        //遍历表格
        for (int i = 0; i < this.gdvSpecialPlan.Rows.Count; i++)
        {
            CheckBox _ckb = (CheckBox)this.gdvSpecialPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            if (_ckb.Checked == true)
            {
                _count++;
                if (this.gdvSpecialPlan.DataKeys[i]["Id"].ToString().Length == 0)
                {
                    object[] _value = new object[15];
                    _value[0] = Convert.ToInt32(this.gdvSpecialPlan.Rows[i].Cells[1].Text);
                    //工作内容
                    _value[1] = this.gdvSpecialPlan.Rows[i].Cells[2].Text;
                    _value[2] = Convert.ToDateTime(this.gdvSpecialPlan.Rows[i].Cells[3].Text);
                    _value[3] = Convert.ToDateTime(this.gdvSpecialPlan.Rows[i].Cells[4].Text);
                    //责任部门
                    _value[4] = this.drpDepartment.SelectedItem.Text;
                    //责任人
                    _value[5] = this.gdvSpecialPlan.Rows[i].Cells[5].Text;
                    //配合不么
                    _value[6] = this.gdvSpecialPlan.Rows[i].Cells[6].Text;
                    //配合责任
                    _value[7] = this.gdvSpecialPlan.Rows[i].Cells[7].Text;
                    //评估人
                    _value[8] = this.gdvSpecialPlan.Rows[i].Cells[8].Text;
                    _value[9] = state;
                    //优先级
                    _value[10] = this.gdvSpecialPlan.Rows[i].Cells[9].Text;
                    //重要程度
                    _value[11] = string.Empty;
                    //审核Id
                    _value[12] = Convert.ToInt32(_VerifyCircuitId.Rows[0]["VerifyCircuitId"].ToString());
                    //发起人
                    _value[13] = objUserInfo.UserName;
                    _value[14] = this.gdvSpecialPlan.Rows[i].Cells[10].Text;
                    _valueList.Add(_value);
                }
            }

        }
        #endregion
        if (_count <= 0)
        {
            if (!IsSubmit)
            {
                Utility.HttpHelp.alert("请选择要保存的计划", Page);
            }
            return;
        }
        if (_valueList.Count == 0)
        {
            if (!IsSubmit)
            {
                Utility.HttpHelp.alert("选择的计划已保存", Page);
                return;
            }
        }
        if (_valueList.Count > 0)
        {
            int result = new BLL.SpecialPlan().addWithTransaction(_valueList);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("添加专项计划未成功，请重试！");
            }
            if (!IsSubmit)
            {
                #region 善后操作
                //将文本框清空
                this.txtSpecialPlanSortId.Value = Convert.ToString(this.gdvSpecialPlan.Rows.Count + 1);
                this.txtCompleteTime.Text = "";
                this.txtRemark.Text = "";
                this.txtSlaveDepartment.Value = "";
                this.txtSlaveDepartmentDuty.Text = "";
                this.txtEvaluatePeople.Value = "";
                this.txtWorkItem.Text = "";
                #endregion
            }
        }
    }

    /// <summary>
    /// 提交月计划方法
    /// </summary>
    /// <param name="state">0草稿 1 提交</param>    
    void subMitMonthPlan(int state)
    {
        //保存
        addSpecialPlan(0, true);
        #region 参数
        int _count = 0;
        List<object[]> _valueList = new List<object[]>();
        //用于保存已提交计划id（用于提示）
        string submitedid = string.Empty;
        for (int i = 0; i < this.gdvSpecialPlan.Rows.Count; i++)
        {
            CheckBox _ckb = (CheckBox)this.gdvSpecialPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            if (_ckb.Checked == true)
            {
                _count++;
                if (this.gdvSpecialPlan.DataKeys[i]["Id"].ToString().Length > 0)
                {
                    object[] _value = new object[2];
                    _value[0] = Convert.ToInt32(this.gdvSpecialPlan.DataKeys[i]["Id"].ToString());
                    _value[1] = objUserInfo.UserName;
                    _valueList.Add(_value);
                    submitedid += this.gdvSpecialPlan.Rows[i].Cells[1].Text + ",";
                }
            }
        }
        #endregion

        if (_valueList.Count > 0)
        {
            int result = new BLL.SpecialPlan().SubmitWithTransaction(_valueList);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("提交专项计划未成功，请重试！");
            }
        }
        else
        {
            Utility.HttpHelp.alert("请至少选择一个", Page);
        }
        if (submitedid.Length > 1)
        {
            submitedid = submitedid.Remove(submitedid.Length - 1);
            Utility.HttpHelp.alert("计划序号" + submitedid + " 已被提交", Page);
        }
        else
        {
            Utility.HttpHelp.alert("提交成功", Page);
        }
    }

    protected void drpDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGdv();
    }

    /// <summary>
    /// 全选
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ckbAllSelect_CheckedChanged(object sender, EventArgs e)
    {
        if (this.ckbAllSelect.Checked == true)
        {
            for (int i = 0; i < this.gdvSpecialPlan.Rows.Count; i++)
            {
                CheckBox _chk = ((CheckBox)this.gdvSpecialPlan.Rows[i].Cells[0].FindControl("ckbSelect"));
                _chk.Checked = true;
            }
        }
        else
        {
            for (int i = 0; i < this.gdvSpecialPlan.Rows.Count; i++)
            {
                CheckBox _chk = ((CheckBox)this.gdvSpecialPlan.Rows[i].Cells[0].FindControl("ckbSelect"));
                _chk.Checked = false;
            }
        }
    }


    /// <summary>
    /// 保存待发
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        addSpecialPlan(0, false);
        BindGdv();

        //清除全选按钮状态
        this.ckbAllSelect.Checked = false;
        #region 善后操作
        //将文本框清空
        this.txtSpecialPlanSortId.Value = Convert.ToString(this.gdvSpecialPlan.Rows.Count + 1);

        #endregion
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        subMitMonthPlan(1);
        BindGdv();
    }

    /// <summary>
    /// 上传附件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string _SpecialPlanId = "";

        int _count = 0;
        for (int i = 0; i < this.gdvSpecialPlan.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvSpecialPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                _SpecialPlanId = this.gdvSpecialPlan.DataKeys[i]["Id"].ToString();
                _count++;
            }
        }
        if (_count == 1)
        {
            //定向到添加年附件路径页面
            string _url = "../WebCommon/AttachmentPath.aspx?PlanId=" + _SpecialPlanId + "&PlanType=20&url=AddSpecialPlan.aspx";
            Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','quarterplandetailmodify','height=40,width=400,top=300,left=300,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');</script>");
        }
        if (_count > 1)
        {
            Utility.HttpHelp.alert("只能选择一个计划", Page);
            return;
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请至少选择一个计划", Page);
            return;
        }
    }
    protected void btnMofify_Click(object sender, EventArgs e)
    {
        int _count = 0;
        int SpecialPlanId = 0;
        for (int i = 0; i < this.gdvSpecialPlan.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gdvSpecialPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                if (this.gdvSpecialPlan.DataKeys[i]["Id"].ToString().Length == 0)
                {
                    Utility.HttpHelp.alert("请保存后再修改！", this.Page);
                    return;
                }
                SpecialPlanId = Convert.ToInt32(this.gdvSpecialPlan.DataKeys[i]["Id"].ToString());
                _count++;
            }
        }
        if (_count == 1)
        {
            //判断是否为评估人，如果该条计划的评估人是当前用户则不能修改该条计划

            Model.UserInfo objUser = new Model.UserInfo();
            object[] _user = new object[1];
            _user[0] = objUser.UserName;
            DataTable _userTable = new BLL.UserInfoManager().get(BLL.UserInfoManager.field.UserName.ToString(), _user);

            object[] _value = new object[1];
            _value[0] = SpecialPlanId;
            DataTable SpecialPlan = new BLL.SpecialPlan().GetSpecialPlanById(_value);
            if (SpecialPlan.Rows.Count > 0)
            {
                if (Convert.ToInt32(SpecialPlan.Rows[0]["State"]) >= 1)
                {
                    Utility.HttpHelp.alert("计划已提交，不允许修改！", Page);
                    return;
                }
            }

            if (_userTable.Rows.Count > 0)
            {
                if (_userTable.Rows[0]["RealName"].ToString() == SpecialPlan.Rows[0]["EvaluatePeople"].ToString())
                {
                    Utility.HttpHelp.alert("评估人不能修改计划", Page);
                }
                else
                {
                    string _url = "ModifySpecialPlan.aspx?Id=" + SpecialPlanId + "&url=AddSpecialPlan.aspx&Department=" + this.drpDepartment.SelectedValue;
                    Page.RegisterStartupScript("windowopen", "<script>window.open('" + _url + "','','height=160,width=1200,top=300,left=220,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
                    return;
                }
            }

        }
        if (_count > 1)
        {
            Utility.HttpHelp.alert("只能修改一个计划", Page);
            return;
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择一个计划", Page);
            return;
        }
    }


    /// <summary>
    /// 添加分解
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnResolve_Click(object sender, EventArgs e)
    {
        //计数器
        int _count = 0;
        //专项计划id 
        string SpecialPlanId = "";
        string SpecialPlanSortId = "";
        //专项计划审核流程id
        int _verifyCircuitId = 0;
        object[] _valueDepartmentName = new object[1];
        _valueDepartmentName[0] = this.drpDepartment.SelectedItem.Text;
        //所在部门
        string _masterDepartment = this.drpDepartment.SelectedItem.Text;
        DataTable _verifyCircuitTable = new BLL.VerifyCircuitManager().getVerifyCircuitIdByDepartment(_valueDepartmentName);
        if (_verifyCircuitTable.Rows.Count == 0)
        {
            Utility.Error.RedirectErrorPage("所在部门无计划流程，请重试");
        }
        else
        {
            _verifyCircuitId = Convert.ToInt32(_verifyCircuitTable.Rows[0]["VerifyCircuitId"].ToString());
        }


        //发起人
        string _sponsor = objUserInfo.UserName;
        //获得所选月计划的id
        for (int i = 0; i < this.gdvSpecialPlan.Rows.Count; i++)
        {
            CheckBox _chk = (CheckBox)this.gdvSpecialPlan.Rows[i].Cells[0].FindControl("ckbSelect");
            if (_chk.Checked == true)
            {
                if (this.gdvSpecialPlan.DataKeys[i]["Id"].ToString().Length > 0)
                {
                    SpecialPlanId = this.gdvSpecialPlan.DataKeys[i]["Id"].ToString();
                    SpecialPlanSortId = this.gdvSpecialPlan.Rows[i].Cells[1].Text;
                }
                _count++;
            }
        }
        if (SpecialPlanId.Length == 0)
        {
            Utility.HttpHelp.alert("计划为草稿状态才可分解", Page);
            return;
        }
        if (_count != 1)
        {
            Utility.HttpHelp.alert("只能选择一个计划", Page);
            return;
        }
        else
        {
            //定向到添加月分解页面
            string _url = "SpecialPlanResolveAdd.aspx?SpecialPlanId=" + SpecialPlanId + "&SpecialPlanSortId=" + SpecialPlanSortId;
            Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','monthplandetailmodify','height=200,width=1200,top=300,left=220,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no');</script>");

        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择一个年计划", Page);

        }
    }


    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDel_Click(object sender, EventArgs e)
    {
        //得到button所在行        
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        int _rowIndex = iRow.RowIndex;//得到该行的索引        

        DataTable specialPlan = CreateDataTable();
        //获取当前gridview的内容导入到datatable
        for (int i = 0; i < this.gdvSpecialPlan.Rows.Count; i++)
        {
            DataRow dr = specialPlan.NewRow();
            dr["Id"] = this.gdvSpecialPlan.DataKeys[i]["Id"].ToString();
            dr["SpecialPlanSortId"] = this.gdvSpecialPlan.Rows[i].Cells[1].Text;
            dr["WorkItem"] = this.gdvSpecialPlan.Rows[i].Cells[2].Text;
            dr["WorkItemDesc"] = string.Empty;
            dr["StartTime"] = this.gdvSpecialPlan.Rows[i].Cells[3].Text;
            dr["CompleteTime"] = this.gdvSpecialPlan.Rows[i].Cells[4].Text;
            dr["MasterDepartment"] = this.drpDepartment.SelectedItem.Text;
            dr["MasterPeople"] = this.gdvSpecialPlan.Rows[i].Cells[5].Text;
            dr["SlaveDepartment"] = this.gdvSpecialPlan.Rows[i].Cells[6].Text;
            dr["SlaveDepartmentDuty"] = this.gdvSpecialPlan.Rows[i].Cells[7].Text;
            dr["EvaluatePeople"] = this.gdvSpecialPlan.Rows[i].Cells[8].Text;
            dr["CheckNote"] = string.Empty;
            dr["EvaluatePeopleVerifyOpinion"] = string.Empty;
            dr["State"] = Convert.ToInt32(this.gdvSpecialPlan.DataKeys[i]["State"]);
            dr["PriorityLevel"] = this.drpPriorityLevel.SelectedItem.Text;
            dr["CompleteState"] = 0;
            dr["CompleteDesc"] = string.Empty;
            dr["VerifyPeople"] = string.Empty;
            dr["VerifyCircuitId"] = 0;
            dr["Sponsor"] = objUserInfo.UserName;
            dr["AttachmengPath"] = string.Empty;
            dr["Remart"] = this.txtRemark.Text;
            specialPlan.Rows.Add(dr);
        }

        //计划如果被保存为草稿，也同时删除
        if (this.gdvSpecialPlan.DataKeys[_rowIndex]["Id"].ToString().Length > 0)
        {
            if (Convert.ToInt32(this.gdvSpecialPlan.DataKeys[_rowIndex]["State"].ToString()) != 0)
            {
                Utility.HttpHelp.alert("已提交的计划不可以删除", Page);
                return;
            }
            specialPlan.Rows[_rowIndex].Delete();
            object[] _value = new object[1];
            _value[0] = Convert.ToInt32(this.gdvSpecialPlan.DataKeys[_rowIndex]["Id"].ToString());
            int result = new BLL.SpecialPlan().DelById(_value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("删除专项计划未成功，请重试！");
            }
        }
        else
        {
            specialPlan.Rows[_rowIndex].Delete();
        }
        DataView dv = new DataView(specialPlan);
        dv.Sort = "SpecialPlanSortId";
        this.gdvSpecialPlan.DataSource = dv;
        this.gdvSpecialPlan.DataBind();
        for (int i = 0; i < this.gdvSpecialPlan.Rows.Count; i++)
        {
            wrapByComma(this.gdvSpecialPlan, i, 6);
            wrapByComma(this.gdvSpecialPlan, i, 9);
        }



        this.txtSpecialPlanSortId.Value = Convert.ToString(this.gdvSpecialPlan.Rows.Count + 1);
    }
    protected void btnImport_Click(object sender, EventArgs e)
    {
        //得到参数 部门名称的DataTable
        object[] _valueDepartmentName = new object[1];
        _valueDepartmentName[0] = this.drpDepartment.SelectedItem.Text;
        DataTable _VerifyCircuitId = new BLL.VerifyCircuitManager().getVerifyCircuitIdByDepartment(_valueDepartmentName);
        if (_VerifyCircuitId.Rows.Count > 0)
        {
            string _url = "ImportSpecialPlan.aspx?VerifyCircuitId=" + Convert.ToInt32(_VerifyCircuitId.Rows[0]["VerifyCircuitId"].ToString());
            Page.RegisterStartupScript("windowopen", "<script>window.open('" + _url + "','','height=200,width=900,top=300,left=300,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
            return;
        }
    }
}